package com.haven.webrobotserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haven.webrobotserver.domain.Project;
import com.haven.webrobotserver.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin("http://localhost:8080")
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @GetMapping("/getAll")
    public JSONObject getAll(){

        JSONObject result = new JSONObject();
        try{

            List<Project> allprojects = projectService.getAllprojects();
            if(allprojects != null){

                String s = JSON.toJSONString(allprojects);
                JSONArray objects = JSON.parseArray(s);


                result.put("status", 200);
                result.put("data",objects);
                result.put("msg","success");
            }else{
                result.put("status", 500);
            }
        }catch (Exception e){
            result.put("status", 500);
        }

        return result;
    }
}
